﻿//----------------------------------------------------------------------------------------------------
// The GameService is responsible for managing all managers.
//----------------------------------------------------------------------------------------------------
package HG
{
	// Mangers
	import HG.Managers.*
	
	// services
	import HG.Services.TickService;
	
	// Debig
	import HG.Debug;
	
	public class GameService
	{
		var m_turnManager:TurnManager;
		var m_levelManager:LevelManager;
		var m_stateManager:StateManager;
		var m_visualManager:VisualManager;
		var m_nodeManager:NodeManager;
		var m_playerManager:PlayerManager;
		var m_actionManager:ActionManager;
		var m_visibilityManager:VisibilityManager;
		var m_gameRuleManager:GameRuleManager;
		var m_resourceManager:ResourceManager;
		var m_inventoryManager:InventoryManager;
		var m_aiManager:AIManager;
		var m_bindingManager:BindingManager;
		var m_notificationManager:NotificationManager;
		var m_cameraManager:CameraManager;
		var m_profileManager:ProfileManager;
		
		static var m_startup:Boolean = false;
		var m_initialized:Boolean;
		
		//----------------------------------------------------------------------------------------------------
		public function GameService()
		{
			Debug.ASSERT(!m_startup, "Attempt to start GameService more than once");
			m_startup = true;
			
			// Register the tick service
			TickService.GetInstance(this, 100);
			
			// Now create managers
			CreateManagers();

			//Initialize();
		}
		
		//----------------------------------------------------------------------------------------------------
		// Create all managers.
		//----------------------------------------------------------------------------------------------------
		public function CreateManagers()
		{
			m_turnManager = new TurnManager();
			m_levelManager = new LevelManager();
			m_stateManager = new StateManager();
			m_visualManager = new VisualManager();
			m_nodeManager = new NodeManager();
			m_playerManager = new PlayerManager();
			m_actionManager = new ActionManager();
			m_visibilityManager = new VisibilityManager();
			m_gameRuleManager = new GameRuleManager();
			m_resourceManager = new ResourceManager();
			m_inventoryManager = new InventoryManager();
			m_aiManager = new AIManager();
			m_bindingManager = new BindingManager();
			m_notificationManager = new NotificationManager();
			m_cameraManager = new CameraManager();
			m_profileManager = new ProfileManager();
		}
		
		//----------------------------------------------------------------------------------------------------
		// Initialize all managers.
		//----------------------------------------------------------------------------------------------------
		public function Initialize()
		{
			m_initialized = true;
			
			m_turnManager.Initialize();
			m_levelManager.Initialize();
			m_stateManager.Initialize();
			m_visualManager.Initialize();
			m_nodeManager.Initialize();
			m_playerManager.Initialize();
			m_actionManager.Initialize();
			m_visibilityManager.Initialize();
			m_gameRuleManager.Initialize();
			m_resourceManager.Initialize();
			m_inventoryManager.Initialize();
			m_aiManager.Initialize();
			m_bindingManager.Initialize();
			m_notificationManager.Initialize();
			m_cameraManager.Initialize();
			m_profileManager.Initialize();
		}
		
		//----------------------------------------------------------------------------------------------------
		// Shutdown all managers.
		//----------------------------------------------------------------------------------------------------
		public function Shutdown()
		{
			TickService.GetInstance(this, 0).Stop();
			
			m_turnManager.Shutdown();
			m_levelManager.Shutdown();
			m_stateManager.Shutdown();
			m_visualManager.Shutdown();
			m_nodeManager.Shutdown();
			m_playerManager.Shutdown();
			m_actionManager.Shutdown();
			m_visibilityManager.Shutdown();
			m_gameRuleManager.Shutdown();
			m_resourceManager.Shutdown();
			m_inventoryManager.Shutdown();
			m_aiManager.Shutdown();
			m_bindingManager.Shutdown();
			m_notificationManager.Shutdown();
			m_cameraManager.Shutdown();
			m_profileManager.Shutdown();
		}
		
		//----------------------------------------------------------------------------------------------------
		// Tick All Managers.
		//----------------------------------------------------------------------------------------------------
		public function OnTick(DeltaTime:Number)
		{
			if (!m_initialized)
				Initialize();
			
			m_turnManager.OnTick(DeltaTime);
			m_levelManager.OnTick(DeltaTime);
			m_stateManager.OnTick(DeltaTime);
			m_visualManager.OnTick(DeltaTime);
			m_nodeManager.OnTick(DeltaTime);
			m_playerManager.OnTick(DeltaTime);
			m_actionManager.OnTick(DeltaTime);
			m_visibilityManager.OnTick(DeltaTime);
			m_gameRuleManager.OnTick(DeltaTime);
			m_resourceManager.OnTick(DeltaTime);
			m_inventoryManager.OnTick(DeltaTime);
			m_aiManager.OnTick(DeltaTime);
			m_bindingManager.OnTick(DeltaTime);
			m_notificationManager.OnTick(DeltaTime);
			m_cameraManager.OnTick(DeltaTime);
			m_profileManager.OnTick(DeltaTime);
		}
		
		//----------------------------------------------------------------------------------------------------
		// Get functions return the managers.
		//----------------------------------------------------------------------------------------------------
		public function GetTurnManager():TurnManager
		{
			return m_turnManager;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetLevelManager():LevelManager
		{
			return m_levelManager;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetStateManager():StateManager
		{
			return m_stateManager;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetVisualManager():VisualManager
		{
			return m_visualManager;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetNodeManager():NodeManager
		{
			return m_nodeManager;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetPlayerManager():PlayerManager
		{
			return m_playerManager;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetActionManager():ActionManager
		{
			return m_actionManager;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetVisiblityManager():VisibilityManager
		{
			return m_visibilityManager;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetGameRuleManager():GameRuleManager
		{
			return m_gameRuleManager;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetResourceManager():ResourceManager
		{
			return m_resourceManager;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetInventoryManager():InventoryManager
		{
			return m_inventoryManager;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetAIManager():AIManager
		{
			return m_aiManager;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetBindingManager():BindingManager
		{
			return m_bindingManager;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetNotificationManager():NotificationManager
		{
			return m_notificationManager;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetCameraManager():CameraManager
		{
			return m_cameraManager;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetProfileManager():ProfileManager
		{
			return m_profileManager;
		}
		
		//----------------------------------------------------------------------------------------------------
		// GameService singleton manager
		//----------------------------------------------------------------------------------------------------
		private static var ms_instance:GameService;
		public static function GetInstance():GameService
		{
			if (ms_instance == null)
			{
				ms_instance = new GameService();
			}
			return ms_instance;
		}
		//----------------------------------------------------------------------------------------------------
	}
}